package com.xyh.flowershop.controller;

import com.xyh.flowershop.enums.LoginEnum;
import com.xyh.flowershop.service.LoginService;
import com.xyh.flowershop.service.RegisterService;
import com.xyh.flowershop.vo.req.user.LoginUser;
import com.xyh.flowershop.vo.req.user.RegisterUserReqVo;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author 小约翰可汗
 * @className WxLogin
 * @createTime 2024/4/1621:10
 */
@CrossOrigin(origins = "*")
@RequestMapping("/login/")
@RestController
@Tag(name = "登录接口")
public class LoginController {

    @Resource(name = "loginServiceImp")
    private LoginService loginService;

    @Resource
    private RegisterService registerService;



    @Operation(summary = "微信一键登录")
    @GetMapping("wx/{code}")
    public ResultRespVo wxLogin(@PathVariable String code){
        LoginUser loginUser = new LoginUser();
        loginUser.setLoginToken(code);
        loginUser.setLoginType(LoginEnum.WX_LOGIN.getCode());
        String token = loginService.login(loginUser);
        return ResultRespVo.success(token);
    }

    @Operation(summary = "其他方式登录")
    @PostMapping("account")
    public ResultRespVo accountLogin(@RequestBody LoginUser loginUser){
        String token = loginService.login(loginUser);
        return ResultRespVo.success(token);
    }

    @Operation(summary = "注册账号")
    @PostMapping("register")
    public ResultRespVo register(@RequestBody RegisterUserReqVo vo){
        return registerService.register(vo) ? ResultRespVo.success("注册成功") : ResultRespVo.error();
    }

    @Operation(summary = "验证码发送")
    @PostMapping("sendVerifyCode/{email}")
    public ResultRespVo sendVerifyCode(@PathVariable  String email){
       registerService.verifyCode(email);
       return ResultRespVo.success("验证码已发送至邮箱，请注意查收");
    }

}
